//FACEBOOK ASYNC CLIENT

//FacebookAsyncUser methods

#include "facebookasync.h"

#include "xmlParser.h"
#include "md5.h"
#include <fstream>
#include <iostream>

//POST FEED ITEM ROUTINES


bool FacebookAsyncUser::PostFeedItem(FACEBOOK_FEEDITEM ItemType, const FacebookFeedItem& FeedItemInfo)
{
	assert(m_CurLoginStage == LOGINSTAGE_SUCCEED);
	assert(m_CurInfoStage == INFOSTAGE_SUCCEED);
	assert(m_CurPhotoStage == PHOTOSTAGE_NOTSTARTED || m_CurPhotoStage == PHOTOSTAGE_FAILED || m_CurPhotoStage == PHOTOSTAGE_SUCCEED);
	
	assert(m_CurNotificationStage != NOTIFSTAGE_INPROGRESS);

	assert(m_CurPostFeedItemStage != POSTFEEDSTAGE_INPROGRESS); 

	assert(m_GenericCallInProgress != true);

	assert(m_HasActiveRequest == false);


	std::list<std::string> Params;

	//aggregating params

	//title
	Params.push_back("title=" + FeedItemInfo.m_TitleFBML);

	//body
	if (FeedItemInfo.m_BodyFBML.size() > 0)
		Params.push_back("body=" + FeedItemInfo.m_BodyFBML);

	//image 1
	if (FeedItemInfo.m_Image1URL.size() > 0)
	{
		Params.push_back("image_1=" + FeedItemInfo.m_Image1URL);

		if (FeedItemInfo.m_Image1DestinationURL.size() > 0)
			Params.push_back("image_1_link=" + FeedItemInfo.m_Image1DestinationURL);
	}

	//image 2
	if (FeedItemInfo.m_Image2URL.size() > 0)
	{
		Params.push_back("image_2=" + FeedItemInfo.m_Image2URL);

		if (FeedItemInfo.m_Image2DestinationURL.size() > 0)
			Params.push_back("image_2_link=" + FeedItemInfo.m_Image2DestinationURL);
	}

	//image 3
	if (FeedItemInfo.m_Image3URL.size() > 0)
	{
		Params.push_back("image_3=" + FeedItemInfo.m_Image3URL);

		if (FeedItemInfo.m_Image3DestinationURL.size() > 0)
			Params.push_back("image_3_link=" + FeedItemInfo.m_Image3DestinationURL);
	}

	//image 4
	if (FeedItemInfo.m_Image4URL.size() > 0)
	{
		Params.push_back("image_4=" + FeedItemInfo.m_Image4URL);

		if (FeedItemInfo.m_Image4DestinationURL.size() > 0)
			Params.push_back("image_4_link=" + FeedItemInfo.m_Image4DestinationURL);
	}


	std::string MethodToCall;

	switch(ItemType)
	{
		case FBFEEDITEM_ACTION:
			{
				MethodToCall = "facebook.feed.publishActionOfUser";
			}
			break;
		case FBFEEDITEM_STORY:
			{
				MethodToCall = "facebook.feed.publishStoryToUser";
			}
			break;
	}

	FacebookAsync::Instance().Helper_PrepareAPICall(MethodToCall, Params, this);

	PushNewRequest();

	m_CurPostFeedItemStage = POSTFEEDSTAGE_INPROGRESS; 

	return true;

}


void FacebookAsyncUser::OnPostFeedItemDone()
{
	assert(m_CurPostFeedItemStage == POSTFEEDSTAGE_INPROGRESS);

	PullCompletedRequest();

	if (m_CurRequestResult.size() < 1)
	{
		FacebookAsync::Instance().SetErrorDescription("FacebookAsync::CurUser_PostFeedItem(). Request failed!");
		m_CurPostFeedItemStage = POSTFEEDSTAGE_FAILED; 
		return;
	}

	std::string::size_type FindLoc = m_CurRequestResult.find("feed_publish");

	if (FindLoc != std::string::npos)
	{
		FacebookAsync::Instance().SetErrorDescription("");
		m_CurPostFeedItemStage = POSTFEEDSTAGE_SUCCEED; 
		return;

	}
	else
	{

		FacebookAsync::Instance().SetErrorDescription("FacebookAsync::CurUser_PostFeedItem(). Request failed. Possible because of invalid params.");
		m_CurPostFeedItemStage = POSTFEEDSTAGE_FAILED; 
		return;
	}

}